/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.taglibs.input;

import com.codemagi.util.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.*;

/**
 *  This class implements the &lt;input:reset&gt; tag, which presents an 
 *  &lt;input type="reset" ... /&gt; form element.
 *
 *  @version 1.0
 *  @author August Detlefsen for CodeMagi, Inc.
 */
public class Reset extends Input {

    public int doStartTag() throws JspException {
	try {
            // sanity check
            if (name == null || name.equals(""))
                throw new JspTagException("invalid null or empty 'name'");

	    // get what we need from the page
	    ServletRequest req = pageContext.getRequest();
	    JspWriter out = pageContext.getOut();

	    // start building up the tag
	    out.println();
	    out.println("<input ");
	    out.println("    type=\"reset\"");
	    out.println("    name=\"" + Util.quote(name) + "\"");
            if (!Utils.isEmpty(id)) {
                out.println("    id=\"" + Util.quote(id) + "\"");
            }

	    //disable the form field if disabled is set
	    if (disabled) {
		out.println("    OnFocus=\"blur();\"");
		appendAttribute("style", "cursor:default;disabled:true;");
	    }

	    // include any attributes we've got here
	    Util.printAttributes(out, attributes);

	    /*
	     * print out the value from the request if it's there, or
	     * use the default value if it's not
	     */
	    String param = getParameter(name, (HttpServletRequest)req);

	    if ( override ) {
		out.println("    value=\"" + dVal + "\"");
	    } else if (param != null) {
		out.println("    value=\"" + Util.quote(param) + "\"");
	    } else if (dVal != null) {
		out.println("    value=\"" + dVal + "\"");
	    }

	    // end the tag
	    out.println("/>");

            //show the error message
            if (showError) {
                String errorMessage = (String)req.getAttribute(name + "_error_message");
                if (errorMessage != null && !"".equals(errorMessage) ) {
                    out.print( "<BR><SPAN CLASS=\"input_error_msg\">" + errorMessage + "</SPAN>");
                }
            }


	} catch (Exception ex) {
	    throw new JspTagException(ex.getMessage());
	}
	return SKIP_BODY;
    }
}
